﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//请先添加引用
using Gift.Model;

namespace Gift.DAL
{
    /// <summary>
    /// 数据访问类ProfessionAreaType。
    /// </summary>
    public class DALProfessionAreaType
    {
        public DALProfessionAreaType()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbProfessionAreaType");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(tbProfessionAreaType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbProfessionAreaType(");
            strSql.Append("Name)");
            strSql.Append(" values (");
            strSql.Append("@Name)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.VarChar,50)};
            parameters[0].Value = model.Name;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public bool isDelete(string id)
        {
            string sSQL = " select 1 from DB_Shoppro where ProfessionAreaTypeId=" + id + " union select 1 from tbCardType where ProfessionAreaTypeId=" + id;
            SqlDataReader dr = DbHelperSQL.ExecuteReader(sSQL);
            if (dr.Read())
            {
                dr.Close();
                return false;
            }
            else
            {
                dr.Close();
                return true;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(tbProfessionAreaType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbProfessionAreaType set ");
            strSql.Append("Name=@Name");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@Name", SqlDbType.VarChar,50)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.Name;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete tbProfessionAreaType ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public tbProfessionAreaType GetModel(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,Name from tbProfessionAreaType ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

           tbProfessionAreaType model = new tbProfessionAreaType();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public tbProfessionAreaType GetModelByCardTypeId(int CardTypeId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ft.Id,ft.[Name] from tbProfessionAreaType ft join tbCardType ct on ct.ProfessionAreaTypeId = ft.Id");
            strSql.Append(" where ct.Id=@CardTypeId ");
            SqlParameter[] parameters = {
					new SqlParameter("@CardTypeId", SqlDbType.Int,4)};
            parameters[0].Value = CardTypeId;

            tbProfessionAreaType model = new tbProfessionAreaType();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,Name ");
            strSql.Append(" FROM tbProfessionAreaType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetListByCompanyId(string companyId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select distinct pt.id,pt.[name] "
                         + " from db_card c  join tbCardType ct on c.cardtypeid = ct.id  "
                         + " join tbProfessionAreaType pt on ct.ProfessionAreaTypeId = pt.id"
                         + " where companyId = " + companyId
                         + " order by pt.[name] ");

            return DbHelperSQL.Query(strSql.ToString());
        }
        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbProfessionAreaType";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}

